import { loginAPI } from '@/api/user'
import { Toast } from 'vant'
import { setToken, getToken, setUserInfo } from '@/utils/storage'
import router from '@/router'
export default {
  namespaced: true,
  state () {
    return {
      token: getToken(),
      userInfo: {}
    }
  },
  mutations: {
    setToken (state, token) {
      state.token = token
      setToken(token)
    },
    setUserInfo (state, obj) {
      state.userInfo = obj
    }
  },
  actions: {
    async login (ctx, obj) {
      try {
        const res = await loginAPI(obj)
        console.log('user', res.data.user)
        if (res.code === 200) {
          Toast.success(res.message)
          router.push('/find')
          setToken(res.data.jwt)
          setUserInfo(res.data.user)
          ctx.commit('setToken', res.data.jwt)
          ctx.commit('setUserInfo', { ...res.data.user })
        } else {
          Toast.fail(res.data.message)
        }
      } catch (error) {
      }
    }
  },
  getters: {}
}
